package com.cn.ccp.action.onlineEdu;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.apache.log4j.Logger;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.CourseBean;
import com.cn.ccp.bean.CourseEvaluationBean;
import com.cn.ccp.util.CCPContants;


/**
 * 其他老师在线课程
 * @author renyongcheng
 *
 */
public class OtherTeachCourse extends BaseAction{
	private Logger log = Logger.getLogger(OtherTeachCourse.class);
	private int otherTotalNumber;
	private String teachId;//老师id
	private List<CourseBean> otherCourseList;//其他老师课程列表
	private String strCourseList;//所有课程列表字符串
	
	public OtherTeachCourse(){
		log.info("---------OnlineTeach()-------");
		teachId = String.valueOf(CCPContants.getUserBean().getId());
		log.info("------------------teachId = "+teachId);
		getpagedetail();
	}
	
	public void getpagedetail() {
		otherTotalNumber = getCourseService().otherCourseCount(teachId);
		log.info("------------------otherTotalNumber = "+otherTotalNumber);
		setItemCount(otherTotalNumber);
		int currentpage = getCurrentPage();
		int pagesize =8;
		this.setPageSize(pagesize);
		int start = (currentpage - 1) * pagesize;
		otherCourseList = getCourseService().otherCourse(teachId, start, pagesize);
		log.info("------------------otherCourseList.size() = "+otherCourseList.size());
		initData();
		 
	}
	 
	private void initData(){
		StringBuffer buf = new StringBuffer();
		int i = 0;
		buf.append("<ul class='thumbnails'>");
		for(CourseBean b : otherCourseList){
			CCPContants.getFilePath(b.getImage() , getFilePathService(),b.getFile_path_id() , CCPContants.getUserBean().getId() ,false,null);
			if(i % 4 == 0 && i != 0){
				buf.append("</ul>");
				buf.append("<ul class='thumbnails'>");
			}
			buf.append("<li class='span3'>");
			
			if(!b.getImage().equals("book-default.png")){
				String fileName = b.getImage().split("/")[1];
				b.setImage(CCPContants.getUserBean().getId()+"/"+fileName);
			}
			
			if(b.isHasSelected())
				buf.append("<img src='tempfile/"+(b.getImage() == null ? "book-default.png'":b.getImage()) +"'  style=\"width:190px;height:156px;\"  alt='' >");
			else	
				buf.append("<a href='pages/course/select/editSelect.faces?courseId="+b.getId()+"&addOnlineTeach=true' class='img'><img src='tempfile/"+(b.getImage() == null ? "book-default.png'":b.getImage()) +"'  style=\"width:190px;height:156px;\"  alt='' ></a>");
			buf.append("<div class='caption'>");
//			log.info("---------"+b.getCourse_name());
			buf.append("<p title=\""+b.getCourse_name()+"\">");
			buf.append(b.getCourse_name().length() > 9 ? b.getCourse_name().substring(0, 9)+ "...<br />" : b.getCourse_name()+"<br />");
			buf.append(getMessages("online_page_select_teacher")+b.getTeacherName()+"<br />");
			if(!b.getStart_level().equalsIgnoreCase("0"))
				buf.append("<span class='text-error'>"+b.getStart_level()+"</span>"+getMessages("online_page_select_star")+"<br />");
			else
				buf.append("<span class='text-error'></span>"+getMessages("online_page_select_star_level_no")+"<br />");
			buf.append("</p>");
			buf.append("</div>");
			if(b.isHasSelected())
				buf.append("&nbsp;&nbsp;<i class ='btn'>"+ getMessages("pages_online_teach_course_has_choose") +"</i>&nbsp;&nbsp;");	
			buf.append("</li>");
			i++;
			log.info("--------------------------------- i = "+i);
		}
		buf.append("</ul>");
		strCourseList = buf.toString();
		log.info(strCourseList);
	}
	
	
	public String submitSelections(ActionEvent event){
		log.info("------------------submitSelections-------------- ");
		List<CourseBean> selectList = new ArrayList<CourseBean>();
		for(CourseBean b : otherCourseList){
			log.info("-----------"+b.isSelected());
			if(b.isSelected())
				selectList.add(b);
		}
//		getCourseService().commitSelectedCourse(selectList,teachId);
		getpagedetail();
		return "backtoteachlist";
	}
	
	public void cancelSelected(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		if(varMap.get("courseId") != null){
			log.info("------------------"+varMap.get("courseId"));
			getCourseService().cancelSelectedCourse(varMap.get("courseId"), teachId);
			getpagedetail();
		}
	}
	
	

	public int getOtherTotalNumber() {
		otherTotalNumber = getCourseService().otherCourseCount(teachId);
		return otherTotalNumber;
	}

	public void setOtherTotalNumber(int otherTotalNumber) {
		this.otherTotalNumber = otherTotalNumber;
	}

	public List<CourseBean> getOtherCourseList() {
		return otherCourseList;
	}

	public void setOtherCourseList(List<CourseBean> otherCourseList) {
		this.otherCourseList = otherCourseList;
	}

	public String getStrCourseList() {
		log.info(strCourseList);
		return strCourseList;
	}

	public void setStrCourseList(String strCourseList) {
		this.strCourseList = strCourseList;
	}
	
	
}
